package com.hlz.aisearch

import android.content.Context
import android.content.Intent
import android.os.Bundle
import android.provider.Settings
import androidx.activity.result.ActivityResultLauncher
import androidx.activity.result.contract.ActivityResultContracts
import androidx.appcompat.app.AppCompatActivity
import androidx.viewpager2.widget.ViewPager2
import com.hlz.comment.base.BaseFragment
import com.hlz.lib_wechat.WeChatLogService
import android.text.TextUtils
import android.text.TextUtils.SimpleStringSplitter
import com.hlz.comment.App


class MainActivity : AppCompatActivity() {

    val urls = arrayOf(
        "https://x5.tencent.com/tbs/index.html",
        "https://x5.tencent.com/tbs/index.html",
        "https://x5.tencent.com/tbs/index.html",
    )

    val requestDataLauncher by lazy {
        registerForActivityResult(ActivityResultContracts.RequestPermission()){granted->
            if (granted){

            }else{

            }
        }
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)


        val viewPager2 = findViewById<ViewPager2>(R.id.mViewPager)
        viewPager2.adapter = WebPageAdapter(getList(),this)
        viewPager2.currentItem = 1

        val s = Intent(this,WeChatLogService::class.java)
        s.setAction("android.accessibilityservice.AccessibilityService")
        s.`package` = applicationInfo.packageName
        startService(s)
    }

    override fun onDestroy() {
        super.onDestroy()
        val s = Intent(this,WeChatLogService::class.java)
        s.setAction("android.accessibilityservice.AccessibilityService")
        s.`package` = applicationInfo.packageName
        stopService(s)
    }

    private fun getList(): List<BaseFragment> {
        val list = arrayListOf<WebFragment>()
        for (url in urls){
            val bundle = Bundle()
            bundle.putString("url",url)
            val fragment = WebFragment.instance(bundle)
            list.add(fragment)
        }
        return list
    }


    private fun isAccessibilitySettingsOn(mContext: Context): Boolean {
        var accessibilityEnabled = 0
        val service = packageName + "/" + WeChatLogService::class.java.getCanonicalName()
        try {
            accessibilityEnabled = Settings.Secure.getInt(
                mContext.getApplicationContext().getContentResolver(),
                Settings.Secure.ACCESSIBILITY_ENABLED
            )
        } catch (e: Settings.SettingNotFoundException) {
        }
        val mStringColonSplitter = SimpleStringSplitter(':')
        if (accessibilityEnabled == 1) {
            val settingValue: String = Settings.Secure.getString(
                mContext.getApplicationContext().getContentResolver(),
                Settings.Secure.ENABLED_ACCESSIBILITY_SERVICES
            )
            if (settingValue != null) {
                mStringColonSplitter.setString(settingValue)
                while (mStringColonSplitter.hasNext()) {
                    val accessibilityService = mStringColonSplitter.next()
                    if (accessibilityService.equals(service, ignoreCase = true)) {
                        return true
                    }
                }
            }
        } else {
        }
        return false
    }



}